package nnfe.userCenter.ctrl;

<#--<#if hasCompositePk&&primeType!=toClassName>import com.bluemobi.ssm.model.${toClassName}Id;</#if>-->
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import nnfe.userCenter.interfaceModel.base.RequestResultSingle;
import nnfe.userCenter.jpa.${toClassName}Repository;
import nnfe.userCenter.model.${toClassName};

/**
 * @author: DGG-XAM3C create on ${.now}
 * @Description: ${toClassName} REST风格接口
 */
@RestController
@RequestMapping("/api/rest/${toEntityName}")
public class ${toClassName}Ctrl {

private static final Logger logger = LoggerFactory.getLogger(${toClassName}Ctrl.class);
@Autowired
private ${toClassName}Repository ${toEntityName}Dao;

/**
* 分页查询
*
* @param page
* @param count
* @return
*/
@RequestMapping("/list/{page}/{count}")
public RequestResultSingle<Page> list(@PathVariable int page, @PathVariable int count) {
RequestResultSingle<Page> result = new RequestResultSingle<>();
Pageable pageable = new PageRequest(page, count);
try {
Page<${toClassName}> queryResult = ${toEntityName}Dao.findAll(pageable);
result.setData(queryResult);
} catch (Exception ex) {
result.setErrorMessage(ex.getMessage());
}
return result;
}

/**
* 根据id查询
*
* @param id
* @return
*/
@RequestMapping(value = "/getById/{id}")
public RequestResultSingle<${toClassName}> get${toClassName}ById(@PathVariable String id) {
RequestResultSingle<${toClassName}> result = new RequestResultSingle<>();
try {
${toClassName} ${toEntityName} = ${toEntityName}Dao.findOne(id);
result.setData(${toEntityName});
} catch (Exception ex) {
result.setErrorMessage(ex.getMessage());
logger.info("=====", ex);
}
return result;
}

/**
* 保存数据
*
* @param ${toEntityName}
* @return
*/
@RequestMapping(value = "/save")
public RequestResultSingle save${toClassName}(${toClassName} ${toEntityName}) {
        RequestResultSingle result = new RequestResultSingle();
        try {
${toEntityName}Dao.save(${toEntityName});
        } catch (Exception ex) {
        result.setErrorMessage(ex.getMessage());
        logger.info("=====", ex);
        }
        return result;
}

/**
* 删除数据
*
* @param id
* @return
*/
@RequestMapping(value = "/remove/{id}")
public RequestResultSingle remove${toClassName}(@PathVariable String id) {
        RequestResultSingle result = new RequestResultSingle();
        try {
${toEntityName}Dao.delete(id);
        } catch (Exception ex) {
        result.setErrorMessage(ex.getMessage());
        logger.info("=====", ex);
        }
        return result;
}

/**
* 修改数据
*
* @param ${toEntityName}
* @return
*/
@RequestMapping(value = "/modify")
public RequestResultSingle modify${toClassName}(${toClassName} ${toEntityName}) {
        RequestResultSingle result = new RequestResultSingle();
        try {
${toEntityName}Dao.save(${toEntityName});
        } catch (Exception ex) {
        result.setErrorMessage(ex.getMessage());
        logger.info("=====", ex);
        }
        return result;
}
}
